package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfControlPointDAO;
import cn.org.timespace.easyframe.model.permission.EfControlPoint;

/**
 * <p>
 * 控制点
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author 
 */

public class EfControlPointDAOImpl extends EfBaseDAO implements EfControlPointDAO {
	
	public List<EfControlPoint> findAll() {
		log.debug("finding all EfControlPoint instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfControlPoint a order by a.efMenu.menuId asc,controlPointId ");
			
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<EfControlPoint> findAllByIsValid(String dicIsValid) {
		log.debug("finding all EfControlPoint instances");
		try {
			StringBuffer queryString = new StringBuffer("from EfControlPoint a where a.dicIsValid = '"+dicIsValid+"' order by a.efMenu.menuId asc,controlPointId ");
			
			return getHibernateTemplate().find(queryString.toString());
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<EfControlPoint> findByUserId(String userId){
		Session session = null;
		Query query = null;
		
		List<EfControlPoint> resList = new ArrayList<EfControlPoint>();
		try {
			StringBuffer queryString = new StringBuffer();
			queryString.append("SELECT A.*                                     ");
			queryString.append("  FROM EF_CONTROL_POINT      A,                ");
			queryString.append("       EF_ROLE_CONTROL_POINT B,                ");
			queryString.append("       EF_ROLE               C,                ");
			queryString.append("       EF_USER_ROLE          D,                ");
			queryString.append("       EF_USER               E                 ");
			queryString.append(" WHERE A.CONTROL_POINT_ID = B.CONTROL_POINT_ID ");
			queryString.append("   AND B.ROLE_ID = C.ROLE_ID                   ");
			queryString.append("   AND C.ROLE_ID = D.ROLE_ID                   ");
			queryString.append("   AND D.USER_ID = E.USER_ID                   ");
			queryString.append("   AND A.DIC_IS_VALID = '1'                    ");
			queryString.append("   AND E.USER_ID = '"+userId+"'                ");
			
			session = getSessionFactory().openSession();
			query = session.createSQLQuery(queryString.toString());
			
			List list = query.list();
			
			if(list != null && list.size() > 0){
				for(int i=0;i<list.size();i++)
				{
					Object[] obj = (Object[])list.get(i);
					EfControlPoint controlPoint = new EfControlPoint();
					controlPoint.setControlPointId(obj[0] != null ? obj[0].toString() : null);
					controlPoint.setControlPointCode(obj[2] != null ? obj[2].toString() : null);
					controlPoint.setControlPointName(obj[3] != null ? obj[3].toString() : null);
					controlPoint.setDicIsValid(obj[4] != null ? obj[4].toString() : null);
					resList.add(controlPoint);
				}
			}
			
			return resList;
			
		} catch (RuntimeException re) {
			log.error("findTopLevelMenu failed", re);
			throw re;
		}finally{
			if(session != null) session.close();
			if(query != null) query = null;
		}
	}
	
}